<template>
    <el-form :model="ruleForm" :rules="rules" label-width="130px" ref="ruleFormRef">
        <!-- 基本信息 -->
        <div class="bg-white mt-[10px]">
            <BaseHead>基本信息</BaseHead>
            <div class="p-[10px]">

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="专利名称" prop="name">
                            <el-input placeholder="请输入专利名称" v-model="ruleForm.name" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属单位" prop="unitId">
                            <el-select v-model="ruleForm.unitId"
                                :disabled="(tableData && tableData[0].disable) || Cookies.get('role-key') == 'biz_kyry'"
                                placeholder="请选择所属单位" clearable>
                                <el-option v-for="item in zcgsdwList" :key="item.codeColumn" :label="item.valueColumn"
                                    :value="item.codeColumn" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="专利类型" prop="typeId">
                            <el-radio-group v-model="ruleForm.typeId">
                                <el-radio :value="item.code" size="large" v-for="item in zllxList" :key="item.code"> {{
        item.name }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="专利状态" prop="stateId">
                            <el-radio-group v-model="ruleForm.stateId">
                                <el-radio :value="item.code" size="large" v-for="item in zlztList" :key="item.code"> {{
        item.name }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请号" prop="applyCode">
                            <el-input placeholder="请输入申请号" v-model="ruleForm.applyCode" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请日" prop="applyDate">
                            <el-date-picker v-model="ruleForm.applyDate" type="date" format="YYYY/MM/DD"
                                value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="ruleForm.stateId == '2' || ruleForm.stateId == '3'">
                        <el-form-item label="公开号">
                            <el-input placeholder="请输入公开号" v-model="ruleForm.openCode" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="ruleForm.stateId == '2' || ruleForm.stateId == '3'">
                        <el-form-item label="公开日">
                            <el-date-picker v-model="ruleForm.openDate" type="date" format="YYYY/MM/DD"
                                value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="ruleForm.stateId == '3'">
                        <el-form-item label="授权公告号">
                            <el-input placeholder="请输入授权公告号" v-model="ruleForm.authorizeCode" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="ruleForm.stateId == '3'">
                        <el-form-item label="授权公告日	">
                            <el-date-picker v-model="ruleForm.authorizeDate" type="date" format="YYYY/MM/DD"
                                value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请人">
                            <el-input placeholder="请输入申请人" v-model="ruleForm.applyInfo" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="ruleForm.stateId == '3'">
                        <el-form-item label="专利权人">
                            <el-input placeholder="请输入专利权人" v-model="ruleForm.patentee" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="ruleForm.stateId == '3'">
                        <el-form-item label="专利权状态">
                            <el-radio-group v-model="ruleForm.invalidFlag">
                                <el-radio value="ineffective" size="large">未生效</el-radio>
                                <el-radio value="yes" size="large">有效</el-radio>
                                <el-radio value="rollout" size="large">已转出</el-radio>
                                <el-radio value="no" size="large">无效</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>

            </div>
        </div>
        <!-- 详细信息 -->
        <div class="bg-white mt-[10px]">
            <BaseHead>详细信息 </BaseHead>
            <div class="p-[10px]">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="合作类型" prop="cooperationType">
                            <el-radio-group v-model="ruleForm.cooperationType">
                                <el-radio :value="item.code" size="large" v-for="item in hzlxList" :key="item.code">
                                    {{
        item.name }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联合申请知识产权协议书附件" label-width="220px" v-if="ruleForm.cooperationType == '2'">
                            <FileUpload :uploadUrl="uploadUrl" v-model="fileList1" @change="fileChange1"></FileUpload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="学校署名" prop="schoolSign">
                            <el-radio-group v-model="ruleForm.schoolSign">
                                <el-radio value="0" size="large">第一单位</el-radio>
                                <el-radio value="1" size="large">非第一单位</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否职务专利" prop="isDuty">
                            <el-radio-group v-model="ruleForm.isDuty">
                                <el-radio value="1" size="large">是</el-radio>
                                <el-radio value="0" size="large">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="专利范围" prop="scopeId">
                            <el-radio-group v-model="ruleForm.scopeId">
                                <el-radio :value="item.code" size="large" v-for="item in zlfwList" :key="item.code">
                                    {{
        item.name }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="国家地区" prop="region" v-if="ruleForm.scopeId == 2">
                            <el-radio-group v-model="ruleForm.region">
                                <el-radio :value="item.code" size="large" v-for="item in gjdqList" :key="item.code">
                                    {{
        item.name }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否代理" prop="isAgency">
                            <el-radio-group v-model="ruleForm.isAgency">
                                <el-radio value="1" size="large">是</el-radio>
                                <el-radio value="0" size="large">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否PCT专利" prop="isPct">
                            <el-radio-group v-model="ruleForm.isPct">
                                <el-radio value="1" size="large">是</el-radio>
                                <el-radio value="0" size="large">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="IPC主分类号" prop="patentIpc">
                            <el-input placeholder="请输入IPC主分类号" v-model="ruleForm.patentIpc" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="专利证书附件" prop="fileIds">
                            <FileUpload :uploadUrl="uploadUrl" v-model="fileList" @change="fileChange"></FileUpload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
        </div>
        <!-- 代理信息 -->
        <div class="bg-white mt-[10px]" v-if="ruleForm.isAgency == '1'">
            <BaseHead>代理信息 </BaseHead>
            <div class="p-[10px]">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="代理公司">
                            <el-input placeholder="请输入代理公司" v-model="ruleForm.agencyName" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="通讯发明人" prop="txAuthor">
                            <el-input placeholder="请输入通讯发明人" v-model="ruleForm.txAuthor" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="通讯发明人电话" prop="txAuthorTel">
                            <el-input placeholder="请输入通讯发明人电话" v-model="ruleForm.txAuthorTel" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="附件">
                            <FileUpload :uploadUrl="uploadUrl" v-model="fileList2" @change="fileChange2"></FileUpload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
        </div>
        <!-- PCT信息 -->
        <div class="bg-white mt-[10px]" v-if="ruleForm.isPct == '1'">
            <BaseHead>PCT信息 </BaseHead>
            <div class="p-[10px]">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="PCT专利名称" prop="pctName">
                            <el-input placeholder="请输入PCT专利名称" v-model="ruleForm.pctName" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="PCT专利申请号" prop="pctCode">
                            <el-input placeholder="请输入PCT专利申请号" v-model="ruleForm.pctCode" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="PCT专利申请日期" prop="pctDate" label-width="140">
                            <el-date-picker v-model="ruleForm.pctDate" type="date" format="YYYY/MM/DD"
                                value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="PCT专利优先权日">
                            <el-date-picker v-model="ruleForm.priorityDate" type="date" format="YYYY/MM/DD"
                                value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="进入国家">
                            <el-input placeholder="请输入进入国家" v-model="ruleForm.enterCountry" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
        </div>
        <!-- 发明（设计）人 -->
        <div class="bg-white mt-[10px]">
            <BaseHead>发明（设计）人
                <template v-slot:Rslot>
                    <el-button @click="addHonor">添加</el-button>
                </template>
            </BaseHead>
            <div class="p-[10px]">
                <el-table :data="tableData" border style="width: 100%">
                    <el-table-column align="center" type="index" label="署名顺序" width="80" />
                    <el-table-column align="center" prop="date" label="发明人类型">
                        <template #default="scope">
                            <el-select v-model="scope.row.authorType"
                                :disabled="scope.row.disable || (Cookies.get('role-key') == 'biz_kyry' && scope.$index == 0)"
                                @change="e => {
        selectAuthorType(e, scope.$index);
    }">
                                <el-option v-for="item in wcrTypelist" :key="item.code" :label="item.name"
                                    :value="item.code" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="name" label="发明人姓名">
                        <template #default="scope">
                            <el-select v-model="scope.row.authorName"
                                :disabled="scope.row.disable || (Cookies.get('role-key') == 'biz_kyry' && scope.$index == 0)"
                                @change="e => {
        selectAuthor(e, scope.$index, scope.row);
    }" v-if="scope.row.authorType != '3'" filterable remote reserve-keyword placeholder="请输入关键字检索"
                                :remote-method="remoteMethod" :loading="loading">
                                <el-option v-for="item in options" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                            <!-- <el-autocomplete v-model="scope.row.authorName" v-else placeholder="请输入关键字检索"
                                value-key="label" :fetch-suggestions="querySearchAsync"
                                @select="e => { handleSelect(e, scope.$index) }" /> -->
                            <el-input v-model="scope.row.authorName" v-else />
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="address" label="学历">
                        <template #default="scope">
                            <el-select v-model="scope.row.eduLevelId">
                                <el-option v-for="item in xlList" :key="item.code" :label="item.name"
                                    :value="item.code" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="address" label="学位">
                        <template #default="scope">
                            <el-select v-model="scope.row.eduDegreeId">
                                <el-option v-for="item in xwList" :key="item.code" :label="item.name"
                                    :value="item.code" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="address" label="工作单位">
                        <template #default="scope">
                            <el-input v-model="scope.row.authorUnit" v-if="scope.row.authorType == '3'" />
                            <el-select v-model="scope.row.authorUnitId" :disabled="scope.row.authorType == '1'" @change="e => {
        changUnit(e, scope.$index);
    }" v-else placeholder=" 请选择所属单位" clearable>
                                <el-option v-for=" item in sydwList" :key="item.codeColumn" :label="item.valueColumn"
                                    :value="item.codeColumn" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="address" label="职称">
                        <template #default="scope">
                            <el-tree-select v-model="scope.row.titleId" node-key="id" :data="zcList"
                                :render-after-expand="false" :props="defaultProps" />
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="address" label="贡献率（%）">
                        <template #default="scope">
                            <el-input placeholder="" v-model.number="scope.row.workRatio" @input="e => {
        handleInput1(e, scope.$index);
    }
        " type="number">
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="address" label="操作">
                        <template #default="scope">

                            <el-link type="primary" :underline="false"
                                v-if="!scope.row.disable&& !(Cookies.get('role-key') == 'biz_kyry' && scope.$index == 0)"
                                @click="delHonor(scope.$index)">删除</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
    </el-form>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import { getPersonDetails } from "@/api/achievement/common.js"
import { getCategory } from "@/api/system/category"
import { getpatentadd, getPatentupdate } from "@/api/achievement/zscq"
import { ElMessage, ElMessageBox } from "element-plus";
import FileUpload from "@/views/achievement/components/FileUpload.vue"
import { onMounted } from 'vue';
import Cookies from 'js-cookie'

const props = defineProps({
    xlList: Array,
    zcList: Array,
    zcgsdwList: Array,
    xwList: Array,
    zlztList: Array,
    hzlxList: Array,
    zlfwList: Array,
    gjdqList: Array,
    sydwList: Array,
})
const defaultProps = {
    children: 'children',
    label: 'label',
}

const zllxList = ref([])
const isEdit = ref(false)


let getCategoryInfoMet = async () => {
    let parms = {
        bizName: "专利类型"
    }
    let res = await getCategory(parms)
    if (res.code == 200) {
        zllxList.value = res.data
    }
}









const tableData = reactive([
    {
        authorType: '1',
        eduLevelId: '',
        eduDegreeId: '',
        authorName: '',
        titleId: '',
        authorUnit: '',
        authorUnitId: '',
        workRatio: null,
        personId: '',   //userId
        orderId: 1,
        authorAccount: "",   //职工号
        sexId: '',
        subjectId: ""
    },
]);
const addHonor = () => {
    tableData.push({
        authorType: '1',
        eduLevelId: '',
        eduDegreeId: '',
        authorName: '',
        titleId: '',
        authorUnit: '',
        authorUnitId: '',
        workRatio: null,
        personId: '',   //userId
        orderId: tableData.length ? tableData[tableData.length - 1].orderId + 1 : 1,
        authorAccount: "",   //职工号
        sexId: '',
        subjectId: ""
    })
}
const delHonor = (index) => {
    tableData.splice(index, 1)
}
const selectAuthorType = (e, index) => {
    let type1 = tableData[index].authorType
    let orderId1 = tableData[index].orderId
    tableData[index] = {
        authorType: type1,
        eduLevelId: '',
        eduDegreeId: '',
        authorName: '',
        titleId: '',
        authorUnit: '',
        authorUnitId: '',
        workRatio: null,
        personId: '',   //userId
        orderId: orderId1,
        authorAccount: "",   //职工号
        sexId: '',
        subjectId: ""
    }
    if (e == '3') {
        tableData[index].personId = '3'
    }
}
const wcrTypelist = reactive([
    {
        code: '1',
        name: '教师'
    },
    {
        code: '2',
        name: '学生'
    },
    {
        code: '3',
        name: '校外'
    },
]);
const ruleForm = reactive({
    name: '',
    unitId: '',
    typeId: '',
    stateId: '',
    applyCode: '',
    applyDate: '',
    applyInfo: '',
    cooperationType: "1",
    schoolSign: "0",
    isDuty: '1',
    scopeId: '1',
    region: '',
    isAgency: '0',
    isPct: '0',
    patentIpc: '',
    agencyName: "",
    txAuthor: '',
    txAuthorTel: null,
    pctName: '',
    pctCode: '',
    pctDate: '',
    openCode: '',
    openDate: '',
    authorizeCode: '',
    authorizeDate: '',
    enterCountry: '',
    priorityDate: '',
    patentee: '',
    invalidFlag: '',
    crednetialFileId: '',
    fileIds: '',
    joinFileId: '',
})
const rules = reactive({
    name: [
        { required: true, message: '请输入专利名称', trigger: 'blur' },
    ],
    applyCode: [
        { required: true, message: '请输入申请号', trigger: 'blur' },
    ],
    // patentIpc: [
    //     { required: true, message: '请输入IPC主分类号', trigger: 'blur' },
    // ],
    txAuthor: [
        { required: true, message: '请输入通讯发明人', trigger: 'blur' },
    ],
    // patentIpc: [
    //     { required: true, message: '请输入IPC主分类号', trigger: 'blur' },
    // ],
    txAuthorTel: [
        { required: true, message: '请输入通讯发明人', trigger: 'blur' },
        { pattern: /^1\d{10}$/, message: '请输入有效的电话号码', trigger: 'blur' }
    ],
    pctName: [
        { required: true, message: '请输入PCT专利名称', trigger: 'blur' },
    ],
    pctCode: [
        { required: true, message: '请输入PCT专利申请号', trigger: 'blur' },
    ],
    typeId: [
        {
            required: true,
            message: '请选择专利类型',
            trigger: 'change',
        },
    ],
    stateId: [
        {
            required: true,
            message: '请选择专利状态',
            trigger: 'change',
        },
    ],
    unitId: [
        {
            required: true,
            message: '请选择所属单位',
            trigger: 'change',
        },
    ],
    applyDate: [
        {
            required: true,
            message: '请选择申请日',
            trigger: 'change',
        },
    ],
    pctDate: [
        {
            required: true,
            message: '请选择PCT专利申请日期',
            trigger: 'change',
        },
    ],
    cooperationType: [
        {
            required: true,
            message: '请选择合作类型',
            trigger: 'change',
        },
    ],
    schoolSign: [
        {
            required: true,
            message: '请选择学校署名',
            trigger: 'change',
        },
    ],
    isDuty: [
        {
            required: true,
            message: '请选择是否职务专利',
            trigger: 'change',
        },
    ],
    scopeId: [
        {
            required: true,
            message: '请选择专利范围',
            trigger: 'change',
        },
    ],
    region: [
        {
            required: true,
            message: '请选择国家地区',
            trigger: 'change',
        },
    ],
    isAgency: [
        {
            required: true,
            message: '请选择是否代理',
            trigger: 'change',
        },
    ],
    fileIds: [
        {
            required: true,
            message: '请上传附件',
        },
    ],
    // isPct: [
    //     {
    //         required: true,
    //         message: '请选择是否PCT专利',
    //         trigger: 'change',
    //     },
    // ],
})
const handleInput1 = (value, index) => {
    if (value !== null) { // 检查是否为null（非数字）或已经是数字且在范围内
        tableData[index].workRatio = Math.max(0, Math.min(100, value)); // 确保值在0到100之间
    } else {
        tableData[index].workRatio = null; // 重置为null，或者你可以设置为一个默认值，比如0或空字符串，取决于你的需求
    }
};

const querySearchAsync = (queryString, cb) => {
    console.log(queryString, 'queryString')
    if (queryString) {
        let pamrs = {
            name: queryString
        }
        getPersonDetails(pamrs).then(res => {
            console.log(res, 'res')
            if (res.code == 200) {
                let arr = res.data;
                arr.map(item => {
                    item.value = item.name;
                    item.label = item.name;
                })
                cb(arr);
            }
        })
    } else {
        cb([]);
    }

    // const results = queryString
    //   ? links.value.filter(createFilter(queryString))
    //   : links.value

    // clearTimeout(timeout)
    // timeout = setTimeout(() => {
    //   cb(results)
    // }, 3000 * Math.random())
}

const handleSelect = (item, index) => {
    console.log(item)
    tableData[index].eduDegreeId = item.eduDegreeId
    tableData[index].personId = item.id
    tableData[index].authorAccount = item.account
    tableData[index].sexId = item.sexId
    tableData[index].subjectId = item.subjectId
    tableData[index].authorUnitId = item.unitId
    tableData[index].authorUnit = item.unitName
    tableData[index].eduLevelId = item.eduLevelId
    tableData[index].titleId = item.titleId
}
const changUnit = (value, index) => {
    console.log(value, 'unitvalue')
    if (item) {
        tableData[index].authorUnit = zcgsdwList.value.find(item => item.codeColumn == value).valueColumn
    }
}
const options = ref([])
const loading = ref(false)
const remoteMethod = (query) => {
    if (query) {
        loading.value = true
        let pamrs = {
            name: query
        }
        getPersonDetails(pamrs).then(res => {
            console.log(res, 'res')
            if (res.code == 200) {
                let arr = res.data;
                arr.map(item => {
                    item.value = item.name + ' ' + item.account + ' ' + item.unitName;
                    item.label = item.name + ' ' + item.account + ' ' + item.unitName;
                })
                options.value = arr
                loading.value = false
            }
        })
    } else {
        options.value = []
    }
}
// unit_id
const selectAuthor = (e, index, row) => {
    console.log(e, index, row)
    console.log(options.value.find(item => item.value == e))
    let userInfo = options.value.find(item => item.value == e)
    let cfuserInfo = tableData.find(item => item.personId == userInfo.id)
    if (cfuserInfo) {
        tableData[index].authorName = ''
        ElMessage({
            message: '此人已在列表中，请勿重复添加',
            type: 'error',
        })
        return;
    } else {
        tableData[index].authorName = userInfo.name
        tableData[index].eduDegreeId = userInfo.eduDegreeId
        tableData[index].personId = userInfo.id
        tableData[index].authorAccount = userInfo.account
        tableData[index].sexId = userInfo.sexId
        tableData[index].subjectId = userInfo.subjectId
        tableData[index].authorUnitId = userInfo.unitId
        tableData[index].authorUnit = userInfo.unitName
        tableData[index].eduLevelId = userInfo.eduLevelId
        tableData[index].titleId = userInfo.titleId
    }
}

const ruleFormRef = ref()
const emit = defineEmits(['closeDiglog', 'nextStepMet', 'endLoading', 'startLoading']);
const submitForm = async (i, j = '0', k) => {
    if (!ruleFormRef.value) return
    await ruleFormRef.value.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')
            console.log(ruleForm, 'ruleForm')
            let params = {
                ...ruleForm,
            }
            if (tableData.length == 0) {
                ElMessage({
                    message: '请添加至少一条发明（设计）人信息',
                    type: 'error',
                })
                return;
            } else if (tableData[0].authorName == '') {
                ElMessage({
                    message: '请添加至少一条发明（设计）人信息',
                    type: 'error',
                })
                return;
            } else {
                let ContributionRate = 0
                tableData.forEach(item => {
                    ContributionRate += item.workRatio
                })
                if (ContributionRate > 100) {
                    ElMessage({
                        message: '贡献率不能大于100',
                        type: 'error',
                    })
                    return;
                }
            }
            params.authorList = tableData
            console.log(params, 'params')
            params.submitType = i ? i : Cookies.get("role-key") == 'biz_kyry' ? '1' : '2'
            if (j == '1') {
                params.submitType = params.checkstatus
            }
            if (k) {
                params.checkstatus = k.checkstatus
                params.isAuditor = k.isAuditor
            }
            emit('startLoading', "")
            if (params.id) {
                getPatentupdate(params).then((res) => {
                    if (res.code == 200) {
                        ElMessage({
                            message: res.msg,
                            type: 'success',
                        })
                        if (isEdit.value || k) {
                            emit('closeDiglog', "")
                            // emit('nextStepMet', tableData, params.id)
                        } else {
                            emit('nextStepMet', tableData, params.id)
                        }
                    } else {
                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }
                    emit('endLoading', "")
                })
            } else {
                getpatentadd(params).then((res) => {
                    if (res.code == 200) {
                        ElMessage({
                            message: res.msg,
                            type: 'success',
                        })
                        ruleForm.id = res.data
                        emit('nextStepMet', tableData, res.data)
                    } else {
                        ElMessage({
                            message: res.msg,
                            type: 'error',
                        })
                    }
                    emit('endLoading', "")
                })
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}
const handleClose = () => {
    tableData = [
        {
            authorType: '1',
            eduLevelId: '',
            authorName: '',
            titleId: '',
            authorUnitId: '',
            workRatio: null
        },
    ]
    ruleForm = {
        // id: '',
        honorSn: '',
        honorNumber: 1,
        name: '',
        productName: '',
        honorUnit: '',
        honorLevelId: "",
        honorGradeId: '',
        honorTypeId: '',
        honorDate: '',
        productMode: '',
        totalUnitNum: '',
        unitId: '',
        cooperationType: 1,
        honorFee: '',
        scale: null,
        completeUnit: '',
        unitOrderId: '',
        subjectClassId: 1,
        subjectId: '',
        projectSourceId: '',
        note: ''
    }
}
const uploadUrl = `/commonFile/uploadFile?modelName=patentApply`;
const fileList = ref([]);
const fileList1 = ref([]);
const fileList2 = ref([]);

const getRuleForm = (i) => {
    console.log(i, 'info')
    // ruleForm = i
    if (i.authorList && i.authorList.length > 0) {
        console.log(tableData, 'tableData')
        tableData.length = 0; // 清空数组
        tableData.push(...i.authorList); // 添加新元素

    }
    if (i.crednetialFileList && i.crednetialFileList.length > 0) {
        fileList.value = i.crednetialFileList.map((item) => {
            return {
                name: item.fileName,
                id: item.id,
            };
        });

    }
    if (i.joinFileList && i.joinFileList.length > 0) {
        fileList1.value = i.joinFileList.map((item) => {
            return {
                name: item.fileName,
                id: item.id,
            };
        });
    }
    if (i.fileList && i.fileList.length > 0) {
        fileList2.value = i.fileList.map((item) => {
            return {
                name: item.fileName,
                id: item.id,
            };
        });
    }
    for (const key in i) {
        ruleForm[key] = i[key];
    }
    isEdit.value = true;
    console.log(ruleForm, 'ruleForm')
}


//上传的文件改变
const fileChange = () => {
    const ids = fileList.value.map((item) => {
        const id = item.response ? item.response.data : item.id;
        return id;
    });
    ruleForm.crednetialFileId = ids.join(",");
};
//上传的文件改变
const fileChange1 = () => {
    const ids = fileList1.value.map((item) => {
        const id = item.response ? item.response.data : item.id;
        return id;
    });
    ruleForm.joinFileId = ids.join(",");
};
//上传的文件改变
const fileChange2 = () => {
    const ids = fileList2.value.map((item) => {
        const id = item.response ? item.response.data : item.id;
        return id;
    });
    ruleForm.fileIds = ids.join(",");
};

const getKyryInfo = (i) => {
    if (i && i.length > 0) {
        console.log(tableData, 'tableData')
        tableData.length = 0;
        tableData.push(...i);
        if (i[0].authorUnitId) {
            ruleForm.unitId = i[0].authorUnitId
        }
    }
}




// 把数据暴露出去供父组件调用
defineExpose({
    submitForm,
    getRuleForm,
    getKyryInfo
})
onMounted(() => {
    getCategoryInfoMet()
})
</script>

<style scoped lang="scss">
@import url("@/views/achievement/index.scss");
</style>
